import modelExtend from 'dva-model-extend'
import * as processtypeService from '../services/processtype'
import * as systemprocessService from '../services/systemprocess'

import { pageModel } from './common'

export default modelExtend(pageModel, {
  namespace: 'pt',
  state: {
    list: [],
    total: null,
    page: null,
    modalVisible: false,
    updateModalVisible: false,
    systemprocessSelect: [],
    updateItem: {},
  },
  reducers: {
    save (state, { payload: { data: list, total, page } }) {
      return { ...state, list, total, page }
    },
    showOnAddProcessTypeBtnClick (state, { payload }) {
      return { ...state, modalVisible: true, systemprocessSelect: payload }
    },
    loadSystemprocessSelect (state, { payload }) {
      console.log(payload)
      return { ...state, systemprocessSelect: payload }
    },
    showOnEditProcessTypeBtnClick (state, { payload }) {
      return { ...state, updateModalVisible: true, updateItem: payload.item }
    },
    hideAddProcessTypeModal (state) {
      return { ...state, modalVisible: false }
    },
    hideUpdateProcessTypeModal (state) {
      return { ...state, updateModalVisible: false }
    },
  },
  effects: {
    * fetch ({ payload: { page = 1 } }, { call, put }) {
      console.log(page)
      const { data } = yield call(processtypeService.fetch, { page })
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.list,
            pagination: {
              current: Number(data.pageNum) || 1,
              pageSize: Number(data.pageSize) || 10,
              total: data.total,
              showSizeChanger: false,
            },
          },
        })
      }
    },
    * remove ({ payload: id }, { call, put }) {
      yield call(processtypeService.remove, id)
      yield put({ type: 'reload' })
    },
    * patch ({ payload: { id, values } }, { call, put }) {
      yield call(processtypeService.patch, id, values)
      yield put({ type: 'reload' })
    },
    * create ({ payload: values }, { call, put }) {
      values.systemCode = values.systemCode.label.join('')
      const result = yield call(processtypeService.create, values)
      if (result.data.code === 400) {
        throw new Error(result.data.message)
      }
      yield put({ type: 'add', payload: result })
    },
    * reload (action, { put, select }) {
      const page = yield select(state => state.pt.pagination.current)
      yield put({ type: 'fetch', payload: { page } })
    },
    * loadSelect ({ payload: result }, { call, put }) {
      const { data } = yield call(systemprocessService.systemprocessNameList)
      if (data) {
        yield put({ type: 'loadSystemprocessSelect', payload: data })
      }
    },
    * onAddProcessTypeBtnClick ({ payload: result }, { call, put }) {
      const { data } = yield call(systemprocessService.systemprocessNameList)
      if (data) {
        yield put({ type: 'showOnAddProcessTypeBtnClick', payload: data })
      }
    },
    * onEditProcessTypeBtnClick ({ payload }, { call, put }) {
      const { data } = yield call(processtypeService.getById, payload)
      if (data) {
        yield put({ type: 'showOnEditProcessTypeBtnClick', payload: { item: data.data } })
      }
    },
    * addProccessType ({ payload: values }, { call, put, select }) {
      console.log(values)
      const result = yield call(processtypeService.create, values)
      if (result.data.code === 400) {
        throw new Error(result.data.message)
      }
      yield put({ type: 'hideAddProcessTypeModal' })
      const page = yield select(state => state.pt.pagination.current)
      yield put({ type: 'fetch', payload: { page } })
    },
    * updateProccessType ({ payload: values }, { call, put, select }) {
      const updateItem = yield select(({ pt }) => pt.updateItem)
      values.id = updateItem.id
      const { data } = yield call(processtypeService.patch, values.id, values)
      if (data) {
        yield put({ type: 'hideUpdateProcessTypeModal' })
        const page = yield select(state => state.pt.pagination.current)
        yield put({ type: 'fetch', payload: { page } })
      }
    },
  },
  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/pt') {
          const payload = location.query || { current: 1, pageSize: 10 }
          dispatch({
            type: 'fetch',
            payload,
          })
          dispatch({
            type: 'loadSelect',
            payload,
          })
        }
      })
    },
  },
})
